<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户昵称">
                <a-input allow-clear placeholder="请输入用户昵称" v-model="queryParam.nickname"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="手机号码">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入手机号码" v-model="queryParam.tel"></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button :value="item.id" :key="item.id" v-for="item of statusList">
            {{ item.title }}
          </a-radio-button>
        </a-radio-group>

        <div style="display: flex">
          <!-- <a-button type="primary" style="margin-right: 10px" @click="handle('edit', 0)">+ 新增</a-button> -->
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('change', 1)">批量通过</a-menu-item>
              <a-menu-item @click="handleAll('change', 2)">批量拒绝</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>

      <a-table
        style="width: 100%; overflow-x: auto"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          current: queryParam.pageNo,
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <div slot="content" slot-scope="text, record">
          <div class="content">
            <div class="img">
              <a-avatar shape="square" :size="48" icon="user" :src="record.logo" />
            </div>

            <div class="txt">
              <h3>{{ record.nickname }}</h3>
              <p>{{ record.tel }}</p>
            </div>
          </div>
        </div>

        <span slot="name" slot-scope="text">
          <a-tooltip placement="top">
            <template slot="title">
              <span>{{ text }}</span>
            </template>
            {{ text }}
          </a-tooltip>
        </span>

        <span slot="address" slot-scope="text">
          {{ text.province + text.city + text.district + text.area || '' }}
        </span>

        <span slot="status" slot-scope="text">
          <a-badge :color="getStatus(text)[1]" :text="getStatus(text)[0]" />
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handle('see', record)">查看</a-button>
          <template v-if="!record.status">
            <a-divider type="vertical" />
            <a-dropdown>
              <a-button type="link" size="small">更多<a-icon type="down" /></a-button>
              <a-menu slot="overlay">
                <a-menu-item key="0">
                  <a-button type="link" size="small" @click="handle('change', record, 1)">通过</a-button>
                </a-menu-item>
                <a-menu-item key="0">
                  <a-button type="link" size="small" @click="handle('change', record, 2)">拒绝</a-button>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </template>
        </span>
      </a-table>

      <ContentDetail ref="pop" title="申请详情">
        <div class="pop" v-if="item">
          <h3>{{ item.nickname }}</h3>

          <div class="con">
            <p>真实姓名：{{ item.name }}</p>
            <p>所在位置：{{ item.province + item.city + item.district + item.area || '' }}</p>
            <p>电话号码：{{ item.tel }}</p>
            <p>状态：{{ getStatus(item.status)[0] }}</p>
            <p v-if="item.status === 2">拒绝原因：{{ item.reason }}</p>
          </div>

          <p>申请时间：{{ item.created_at }}</p>

          <div class="des">
            <img :src="item.logo" alt="" />
          </div>

          <a-card title="表单信息" v-if="item.form && item.form.list && item.form.list.length" style="width: 100%; margin-top: 18px">
              <div :key="index" class="goodsItem">
                <h3 style="margin-bottom:20px;">{{ item.form.name }}</h3>

                <a-descriptions size="small" :key="index">
                  <template v-for="cItem of item.form.list">
                    <a-descriptions-item :label="cItem.name" :key="cItem.id">
                      <a-popover v-if="cItem.type === 'uploadImg'" title="" trigger="hover" placement="top">
                        <a-button type="primary"> 查看图片 </a-button>
                        <template slot="content">
                          <img style="height: 200px" :src="cItem.default" alt="" />
                        </template>
                      </a-popover>
                      <span v-else>
                        {{ cItem.default }}
                      </span>
                    </a-descriptions-item>
                  </template>
                </a-descriptions>
              </div>
          </a-card>
        </div>
      </ContentDetail>

      <pagePopForm ref="pagePopForm" @callback="getList()"></pagePopForm>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { workerList, workerStatus } from '@/api/marketing/serviceHome'
import ContentDetail from '@/components/tools/ContentDetail'
import pagePopForm from './components/page-pop-form.vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '用户信息',
    scopedSlots: {
      customRender: 'content',
    },
    width: 300,
  },
  {
    title: '真实姓名',
    dataIndex: 'name',
    scopedSlots: {
      customRender: 'name',
    },
    ellipsis: true,
  },
  {
    title: '地区',
    scopedSlots: {
      customRender: 'address',
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '申请时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 180,
  },
]

export default {
  components: {
    ContentDetail,
    pagePopForm,
  },
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      item: null,
      queryParam: {
        nickname: '',
        status: '-1',
        tel: '',
      },
      list: [],
      selectedRowKeys: [],
      statusList: [
        { id: '-1', title: '全部' },
        { id: '0', title: '待审核' },
        { id: '1', title: '已通过' },
        { id: '2', title: '已拒绝' },
      ],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '0':
            text = '待审核'
            color = '#FAAD14'
            break

          case '1':
            text = '已通过'
            color = '#65C817'
            break

          case '2':
            text = '已拒绝'
            color = '#FF4D4F'
            break
        }

        return [text, color]
      }
    },
  },

  methods: {
    // 选中多行
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    // 批量操作
    handleAll(type, params) {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      switch (type) {
        case 'change':
          this.status({
            id: selectedRowKeys.join(','),
            status: params,
          })
          break
      }
    },

    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await workerList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.pageNo = pageNo
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handle(type, item, parmas) {
      switch (type) {
        case 'change':
          if (parmas === 2) {
            this.$refs.pagePopForm.visible = true
            this.$refs.pagePopForm.form = {
              id: item.id,
              reason: '',
              status: 2,
            }
            return
          }

          this.status({
            id: item.id,
            status: parmas,
          })
          break

        case 'see':
          let _el = this.$refs.pop
          _el.visible = true
          this.item = item
          break
      }
    },

    status(data) {
      this.$confirm({
        title: '提示?',
        content: '是否执行此次操作？',
        onOk: async () => {
          const hide = this.$message.loading('请稍等...')
          const res = await workerStatus(data)
          hide()
          if (res.code) {
            this.$error({
              content: res.msg,
            })
            return
          }
          this.$message.success(res.msg)
          this.getList()
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}

.pop {
  color: #000;

  h3 {
    font-weight: bold;
    font-size: 20px;
  }

  p {
    margin: 0;
    line-height: 22px;
  }

  .con {
    padding: 19px 0 10px;
  }

  .des {
    padding-top: 10px;
    img {
      width: 48px;
      height: 48px;
      border-radius: 4px;
      object-fit: cover;
    }
  }
}
</style>
